package com.example.demo.service.impl;

import com.example.demo.entity.Instorage;
import com.example.demo.entity.Storage;
import com.example.demo.mapper.StorageMapper;
import com.example.demo.service.InstorageService;
import com.example.demo.service.StorageService;
import com.example.demo.service.helper.StorageInventory;
import com.example.demo.service.helper.StorageStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class StorageServiceImpl implements StorageService {

    @Autowired
    private StorageMapper storageMapper;

    @Autowired
    private InstorageService instorageService;

    @Override
    public int createStorage(Storage storage) {
        // 设置默认值
        storage.setIsdeleted(0);
        if (storage.getSquantity() == null) {
            storage.setSquantity(0);
        }
        if (storage.getSweight() == null) {
            storage.setSweight(0.0);
        }
        return storageMapper.createStorage(storage);
    }

    @Override
    public int deleteStorage(Integer sid) {
        // 检查是否有入库记录
        List<Instorage> instorages = instorageService.findInstoragesByStorageId(sid);
        if (!instorages.isEmpty()) {
            throw new RuntimeException("仓库有入库记录，不能直接删除");
        }
        return storageMapper.deleteStorage(sid);
    }

    @Override
    public void logicalDeleteStorage(Integer sid) {
        storageMapper.updateIsdeleted(sid);
    }

    @Override
    public int updateStorage(Storage storage) {
        return storageMapper.updateStorage(storage);
    }

    @Override
    public Storage findStorageById(Integer sid) {
        return storageMapper.findStorageById(sid);
    }

    @Override
    public List<Storage> findAllValidStorages() {
        return storageMapper.findAllStorages();
    }

    @Override
    public List<Storage> findStoragesByCompanyId(Integer cid) {
        return storageMapper.findStoragesByCompanyId(cid);
    }

    @Override
    public List<Storage> findStoragesByPort(String portName) {
        return storageMapper.findAllStorages().stream()
                .filter(s -> portName.equals(s.getShbelong()))
                .collect(Collectors.toList());
    }

    @Override
    public StorageStatus getStorageStatus(Integer sid) {
        Storage storage = storageMapper.findStorageById(sid);
        StorageInventory inventory = instorageService.getStorageInventory(sid);

        return new StorageStatus(
                storage.getSid(),
                storage.getShbelong(),
                storage.getScbelong(),
                inventory.getTotalWeight(),
                inventory.getTotalQuantity(),
                storage.getSweight(),  // 最大容量
                storage.getSquantity() // 最大件数
        );
    }

    @Override
    public boolean checkStorageCapacity(Integer sid, double additionalWeight, int additionalQuantity) {
        StorageStatus status = getStorageStatus(sid);
        return (status.getCurrentWeight() + additionalWeight) <= status.getMaxWeight() &&
                (status.getCurrentQuantity() + additionalQuantity) <= status.getMaxQuantity();
    }
}